unit dpt_import;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, ZAbstractTable, ZDataset,
  ZAbstractRODataset, ZAbstractDataset, ZConnection, ComCtrls, Unit_External,ComObj,ActiveX,
  Grids, Daftar_Desa, ExtCtrls, Setting, JvThread, JvComponentBase,
  JvThreadDialog, Unit7;

  const
  sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF}
               {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};

type
  //-----------
  {TProgressProc = procedure (aProgress: Integer) of object; // 0 to 100
  TProgressThread = class(TThread)
  private
    FProgressProc: TProgressProc;
    FProgressValue: integer;
    procedure SynchedProgress;
  protected
    procedure Progress(aProgress: integer); virtual;
  public
    constructor Create(aProgressProc: TProgressProc; CreateSuspended: Boolean = False); reintroduce; virtual;
  end;
  TMyThread = class(TProgressThread)
  protected
    procedure Execute; override;
  end;}
  //-----------
  CharSet=Set of Char;
  Tdpt_import_form = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    OpenDialog1: TOpenDialog;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    Panel1: TPanel;
    Button1: TButton;
    fileloc: TEdit;
    Panel2: TPanel;
    preview: TStringGrid;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    cbAgama: TComboBox;
    cbAlamat: TComboBox;
    cbIbu: TComboBox;
    cbKawin: TComboBox;
    cbKelainan: TComboBox;
    cbKelamin: TComboBox;
    cbKelamin2: TComboBox;
    cbKet: TComboBox;
    cbNama: TComboBox;
    cbNIK: TComboBox;
    cbNKK: TComboBox;
    cbRt: TComboBox;
    cbRw: TComboBox;
    cbTempat: TComboBox;
    cbTgl: TComboBox;
    cbDusun: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    lblProses: TLabel;
    Panel4: TPanel;
    listexcel: TStringGrid;
    Button3: TButton;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    cbKabkot: TComboBox;
    cbKec: TComboBox;
    cbDesa: TComboBox;
    Hapus: TButton;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    JvThread1: TJvThread;
    JvThreadSimpleDialog1: TJvThreadSimpleDialog;
    Label24: TLabel;
    Label7: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    btnOpen: TButton;
    Button2: TButton;
    Label27: TLabel;
    Label28: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    buttonImport: TButton;
    Label35: TLabel;
    Label36: TLabel;
    desa_terimport: TLabel;
    JvThread2: TJvThread;
    JvThreadSimpleDialog2: TJvThreadSimpleDialog;
    Label32: TLabel;
    Label29: TLabel;
    Edit1: TEdit;
    Label30: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure cbNIKChange(Sender: TObject);
    procedure cbNKKChange(Sender: TObject);
    procedure cbNamaChange(Sender: TObject);
    procedure cbTempatChange(Sender: TObject);
    procedure cbTglChange(Sender: TObject);
    procedure cbIbuChange(Sender: TObject);
    procedure cbAgamaChange(Sender: TObject);
    procedure cbKawinChange(Sender: TObject);
    procedure cbKelaminChange(Sender: TObject);
    procedure cbKelamin2Change(Sender: TObject);
    procedure cbKelainanChange(Sender: TObject);
    procedure cbAlamatChange(Sender: TObject);
    procedure cbRwChange(Sender: TObject);
    procedure cbRtChange(Sender: TObject);
    procedure cbKetChange(Sender: TObject);
    //function filterSpaceName(name:string;tempat:Integer):string;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure buttonImportClick(Sender: TObject);
    procedure cbDusunChange(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure refreshDesa();
    procedure Button3Click(Sender: TObject);
    procedure refreshGrid();
    procedure listexcelClick(Sender: TObject);
    procedure importfileexcel(namafile:string; kdesa:String);
    procedure cbKabkotChange(Sender: TObject);
    procedure cbKecChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);
    procedure HapusClick(Sender: TObject);
    procedure JvThread1Execute(Sender: TObject; Params: Pointer);
    procedure FormDestroy(Sender: TObject);
    procedure JvThread2Execute(Sender: TObject; Params: Pointer);
  private
    { Private declarations }
    function StripCharsInSet(s: string; c: CharSet): string;
	procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    function cekRTRW(kata: String): String;
  public
    { Public declarations }
    daftar_desa : TTDaftar_Desa;
    constructor Create(AOwner: TComponent; kode_login: String);             overload;
  end;

var
  Form2: Tdpt_import_form;
  sFile:String;
  ikabkot,ikec,idesa,sdesa:String;
  sConnectionString: String;
  slTables: TStringList;
  i, j: Integer;
  fField: TField;
  sLine: String;
  sel,namatabel:String;
  tulis:Integer;
  kotak:array of Integer;
  isi:array of String;
  ext:ExternalFile;
  listexceldata,listexceldesa, listnamakota,listnamakec,listnamadesa:tstringlist;
  namakota,namakec,namadesa:string;
  kode_dropdown:string; // kode untuk membatasi kode kabupaten sesuai dengan data login
  //add_double_data:boolean; // penanda apakah data excel ditambah lagi

implementation

{$R *.dfm}

procedure Tdpt_import_form.cbKabkotChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
  namakota:=pilihan;
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan);
    Filtered := True;
    Active := True;
    First;
    iKabkot := FieldByName('KODE').AsString;
    // cb2
    Active := False;
    TableName := 'kecamatan';
    Filter := 'KODE_KABKOT = ' + QuotedStr(iKabkot);
    Filtered := True;
    Active := True;
    First;
  end;
  cbKec.Clear;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure Tdpt_import_form.cbKecChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKec.Items[cbKec.ItemIndex];
  namakec:=pilihan;
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kecamatan';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KABKOT = ' + QuotedStr
      (iKabkot);
    Filtered := True;
    Active := True;
    First;
    iKec := FieldByName('KODE_KECAMATAN').AsString;
    // cb2
    Active := False;
    TableName := 'desa';
    Filter := 'KODE_KECAMATAN = ' + QuotedStr(iKec);
    Filtered := True;
    Active := True;
    First;
  end;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbDesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure Tdpt_import_form.cbDesaChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbDesa.Items[cbDesa.ItemIndex];
  namadesa:=pilihan;
  with ZTable1 do
  begin
    Active := False;
    TableName := 'desa';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KECAMATAN = ' +
      QuotedStr(iKec);
    Filtered := True;
    Active := True;
    First;
    iDesa := FieldByName('KODE_DESA').AsString;
  end;
end;

constructor Tdpt_import_form.Create(AOwner: TComponent; kode_login: String);
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
inherited Create(AOwner);
kode_dropdown:=kode_login;
//    Sfile:=filebrowse;
////    ShowMessage(Sfile);
//  XLApp := CreateOleObject('Excel.Application');
//  try
//    XLApp.Visible := False;
//    XLApp.Workbooks.Open(filebrowse);
//    Sheet := XLApp.Workbooks[ExtractFileName(filebrowse)].WorkSheets[1];
//    Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
//    x := XLApp.ActiveCell.Row;
//    y := XLApp.ActiveCell.Column;
//
//    RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
//
//    preview.ColCount:=y;
//    preview.RowCount:=10;
//    for r := 0 to y do
//      begin
//        preview.Cells[r,0]:=Chr(65+r);
//      end;
//    k := 1;
//    repeat
//      for r := 1 to y do
//      begin
//        //this section to operate data, change as your need
//        preview.Cells[r-1,k]:=RangeMatrix[K, R];
//      end;
//      Inc(k, 1);
//    until ((k > x) or (k>10));
//	for i := 0 to preview.ColCount - 1 do
//    AutoSizeCol(preview, i);
//  RangeMatrix := Unassigned;
//
//  finally
//    if not VarIsEmpty(XLApp) then
//    begin
//      XLApp.Quit;
//      XLAPP := Unassigned;
//      Sheet := Unassigned;
//    end;
//  end;
end;

procedure Tdpt_import_form.refreshGrid();
begin
  //
  listexcel.ColCount := 5;
  listexcel.RowCount := listexceldata.Count+1;
  listexcel.Cells[0,0] := 'No';
  listexcel.Cells[1,0] := 'Kabupaten/Kota';
  listexcel.Cells[2,0] := 'Kecamatan';
  listexcel.Cells[3,0] := 'Desa';
  listexcel.Cells[4,0] := 'Nama File';
  listexcel.ColWidths[1] := 100;
  listexcel.ColWidths[2] := 70;
  listexcel.ColWidths[3] := 70;
  listexcel.ColWidths[4] := 70;
  for I := 1 to listexceldata.Count do
    begin
      listexcel.Cells[0,I] := inttostr(i);
      listexcel.Cells[1,i] := listnamakota[i-1];
      listexcel.Cells[2,i] := listnamakec[i-1];
      listexcel.Cells[3,i] := listnamadesa[i-1];
      listexcel.Cells[4,i] := listexceldata[i-1];
    end;
  for i := 0 to listexcel.ColCount - 1 do
    AutoSizeCol(listexcel, i);
end;
procedure Tdpt_import_form.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

//THREAD
{constructor TProgressThread.Create(aProgressProc: TProgressProc; CreateSuspended: Boolean = False);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := True;
  FProgressProc := aProgressProc;
end;
procedure TProgressThread.Progress(aProgress: Integer);
begin
  FProgressValue := aProgress;
  Synchronize(SynchedProgress);
end;
procedure TProgressThread.SynchedProgress;
begin
  if Assigned(FProgressProc) then
    FProgressProc(FProgressValue);
end;
procedure TMyThread.Execute;
var I: Integer;
begin
  Progress(0);
  Progress(0);
  for I := 1 to 100 do
  begin
    Sleep(10);
    Progress(I);
  end;
end;
procedure Tdpt_import_form.UpdateProgressBar(aProgress: Integer);
begin
  ProgressBar1.Position := aProgress;
  ProgressBar1.Update; // Make sure to repaint the progressbar
end;
//THREAD

function Tdpt_import_form.filterSpaceName(name: string; tempat: Integer):string;
var return :string;
  temp:string;
  daerah:string;
  A:TStringList;
  B:Tstringlist;
begin
  if(tempat=1)then // khusus kota
  begin
    A:=TStringList.Create;
    ExtractStrings([' '], [], PChar(name), A);
    daerah:='';
    for I := 1 to A.Count - 1 do
    begin
      if(I<>1)then
        daerah:=daerah+' ';
      daerah:= daerah+A[I];
    end;
    return := A[0];
    //cek
    ZTable1.Active := False;
    ZTable1.TableName := 'kabkot';
    ZTable1.Filter := 'NAMA = '+QuotedStr(daerah);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    if (ZTable1.Fields[1].AsString = '') then // ga ketemu
    begin
      temp := StringReplace(daerah, ' ', '',
                          [rfReplaceAll, rfIgnoreCase]);
      //ShowMessage('hasil replace : '+temp);
      ZTable1.Active := False;
      ZTable1.TableName := 'kabkot';
      ZTable1.Filter := 'NAMA = '+QuotedStr(temp);
      ZTable1.Filtered := True;
      ZTable1.Active := True;
      ZTable1.First;
      if (ZTable1.Fields[1].AsString = '') then // ga ketemu
      begin
        return:=name;
      end
      else
        return:=return+temp;

    end
    else
      return:=return+name;


  end
  else if(tempat=2)then
  begin
    daerah:=name;

    //cek
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'NAMA = '+QuotedStr(daerah);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    if (ZTable1.Fields[1].AsString = '') then // ga ketemu
    begin
//      temp := StringReplace(daerah, ' ', '',
//                          [rfReplaceAll, rfIgnoreCase]);
//          ShowMessage(daerah+' ga ada');
        temp:='';
        B:=TstringList.Create;
        ExtractStrings([' '], [], PChar(daerah), B);
        for I := 0 to B.Count - 1 do // check satu per satu
        begin
          temp:=temp+B[I];
        end;
//      ShowMessage('hasil replace : '+temp);
      ZTable1.Active := False;
      ZTable1.TableName := 'kecamatan';
      ZTable1.Filter := 'NAMA = '+QuotedStr(temp);
      ZTable1.Filtered := True;
      ZTable1.Active := True;
      ZTable1.First;
      if (ZTable1.Fields[1].AsString = '') then // ga ketemu
      begin
        return:=name;
//        ShowMessage(temp+' ga ada');
      end
      else
        return:=return+temp;
//        ShowMessage(temp+' ada');

    end
    else
      return:=return+name;
//    ShowMessage('hasil return : '+return);

  end
  else if(tempat=3)then
  begin
    daerah:=name;

  //cek
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'NAMA = '+QuotedStr(daerah);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    if (ZTable1.Fields[1].AsString = '') then // ga ketemu
    begin
      temp := StringReplace(daerah, ' ', '',
                          [rfReplaceAll, rfIgnoreCase]);
      //ShowMessage('hasil replace : '+temp);
      ZTable1.Active := False;
      ZTable1.TableName := 'desa';
      ZTable1.Filter := 'NAMA = '+QuotedStr(temp);
      ZTable1.Filtered := True;
      ZTable1.Active := True;
      ZTable1.First;
      if (ZTable1.Fields[1].AsString = '') then // ga ketemu
      begin
        return:=name;
      end
      else
        return:=return+temp;

    end
    else
      return:=return+name;
  end;
  result:=return;
//  ShowMessage('result '+return);
end;   }

function Tdpt_import_form.StripCharsInSet(s:string; c:CharSet):string;
  var i,j:Integer;
  begin
     SetLength(result,Length(s));
     j:=0;
     for i:=1 to Length(s) do
       if (s[i] in c) then
        begin
         inc(j);
         result[j]:=s[i];
        end;
     SetLength(result,j);
  end;

procedure Tdpt_import_form.btnOpenClick(Sender: TObject);

begin
//    if(sFile='')then
//    begin
//      OpenDialog1.InitialDir := GetCurrentDir;
//      // Have the user select an Excel file to open
//      if OpenDialog1.Execute then
//      begin
//        // Get the file they selected
//        sFile := OpenDialog1.FileName;  //ganti folder dialog??
//      end;
//    end;
//

    //    ShowMessage(Sfile);
    if(sfile<>'')then
    begin
      JvThread2.ThreadDialog := JvThreadSimpleDialog2;
      JvThread2.Execute(Self);
    end
    else
    ShowMessage('Anda belum memilih file yang akan diimport');
end;

procedure Tdpt_import_form.Button1Click(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
begin
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);

  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;

  // Only allow existing files to be selected
  openDialog.Options := [ofFileMustExist];

  // Allow only .dpr and .pas files to be selected
  openDialog.Filter := 'Excel files|*.xls';
   // 'Delphi project files|*.dpr|Delphi pascal files|*.pas';

  // Select pascal files as the starting filter type
  //openDialog.FilterIndex := 2;

  // Display the open file dialog
  if openDialog.Execute
  then
  begin
    //ShowMessage('File : '+openDialog.FileName);
    sfile:=openDialog.FileName;
    fileloc.Text:=openDialog.FileName;
  end;

  // Free up the dialog
  openDialog.Free;
end;

procedure Tdpt_import_form.Button2Click(Sender: TObject);
begin
  refreshDesa();
  daftar_desa:=TTDaftar_Desa.Create(Application);
end;

procedure Tdpt_import_form.Button3Click(Sender: TObject);
var find :bool;
begin
  if (ikabkot<>'') and (ikec<>'') and (idesa<>'') and (sfile<>'')then begin
    find:=false;
    for I := 0 to listexceldata.Count - 1 do
    begin
      if(listexceldata[i]=sfile)then
      begin
        find:=true;
        break;
      end;
    end;
    if(find<>true)then
    begin
      listexceldata.Add(sfile);
      listexceldesa.Add(idesa);
      listnamakota.Add(namakota);
      listnamakec.Add(namakec);
      listnamadesa.Add(namadesa);
//      ikec:='';
//      cbKec.Clear;
      idesa:='';
      cbDesa.ItemIndex:=-1;
      sFile:='';
      fileloc.Text:='';
    end;
    refreshGrid();
  end
    else
  ShowMessage('Pilih kota, kecamatan, desa dan file lebih dahulu');
end;



procedure Tdpt_import_form.buttonImportClick(Sender: TObject);
var
  sTemp:String;
  stTemp:TStrings;
  sPath: TStrings;
  kota: String;
  kec: String;
  desa: String;
  tdesa:String;
  idkota: String;
  kkota:String;
  idkec: String;
  idesa: String;
  ktemp:String;
  tanggal:String;
  idtgl:String;
  rwt:String;
  nama,ibu,tptlhr,tglhr,idsimpan,iuser:String;
  iduser:Integer;
  ins:Boolean;
  k:Integer;
  rcount:Integer;
  istableexist:Boolean;
  ext:ExternalFile;
  desaexist:boolean;
begin

  desaexist:=false;
  if (listexceldata.Count>0) then begin
    for I := 0 to listexceldata.Count - 1 do
      begin
        with ztable1 do
        begin
          active:=false;
          tablename:='penyimpan_dpt';
          filter:='NAMA='+ QuotedStr('dpt_'+listexceldesa[i]);
          filtered:=true;
          active:=true;
          first;
          if(RecordCount>0)then
          begin
            desaexist:=true;
            break;
          end;
        end;
      end;
      if(desaexist)then
      begin
        if(MessageDlg('Ada desa yang akan diimport sudah pernah diimport sebelumnya. Apakah anda yakin akan mengimport file ini? Jika ya, maka data akan ditambahkan.',mtCustom,
                                [mbYes,mbCancel], 0)=mryes)then
        begin
          JvThread1.ThreadDialog := JvThreadSimpleDialog1;
          JvThread1.Execute(Self);
        end;
      end
      else
      begin
          JvThread1.ThreadDialog := JvThreadSimpleDialog1;
          JvThread1.Execute(Self);
      end;
  end;


//  if (listexceldata.Count>0) then begin
////    if(sFile='')then
////    begin
////      OpenDialog1.InitialDir := GetCurrentDir;
////      // Have the user select an Excel file to open
////      if OpenDialog1.Execute then
////      begin
////        // Get the file they selected
////        sFile := OpenDialog1.FileName;  //ganti folder dialog??
////      end;
////    end;
////    if(sFile<>'')then
////    begin
//      lblProses.Caption:='Harap tunggu sedang proses';
//      ShowMessage('Proses Import akan memakan waktu yang cukup lama, harap tunggu dan mohon tidak melakukan aksi apapun pada program');
//      //
//      for I := 0 to listexceldata.Count - 1 do
//        begin
//          //ext.importfileexcel(listexceldata[i],listexceldesa[i], ztable1,zquery1,adoconnection1,adoquery1, cbTempat.ItemIndex,cbTgl.ItemIndex);
//          importfileexcel(listexceldata[i],listexceldesa[i]);
//        end;
//      lblProses.Caption:='Selesai';
//      ShowMessage('Excel '+namatabel+' selesai diproses');
////    end;
//    refreshdesa();
//  end
//    else
//      ShowMessage('Tambahkan dulu file excel desa');
end;



procedure Tdpt_import_form.importfileexcel(namafile:string; kdesa:String);
var
  sTemp:String;
  stTemp:TStrings;
  sPath: TStrings;
  kota: String;
  kec: String;
  desa: String;
  tdesa:String;
  idkota: String;
  kkota:String;
  idkec: String;
  ikdesa: String;
  ktemp:String;
  tanggal:String;
  idtgl:String;
  rwt:String;
  nama,ibu,tptlhr,tglhr,idsimpan,iuser:String;
  iduser:Integer;
  ins:Boolean;
  k:Integer;
  rcount:Integer;
  istableexist:Boolean;
  debug:boolean;
  counterbaris:integer;
begin
      debug:=false;
      idkota:= Copy(kdesa,1,2);
      idkec := Copy(kdesa,1,5);
      ikdesa := kdesa;

      with ztable1 do
      begin
        active:=false;
        tablename:='penyimpan_dpt';
        filter:='NAMA='+quotedstr('dpt__'+ikdesa);
        filtered:=true;

        active:=true;
        first;
        if(FieldByName('ID').AsString='')then
        begin
          istableexist:=false; // table belum ada
          //Showmessage(ikdesa+' not exist');
        end
        else
        begin
          if(debug) then Showmessage(ikdesa+' exist');
          //if(MessageDlg('Desa yang akan diimport sudah pernah diimport sebelumnya. Apakah anda yakin akan mengimport file ini? Jika ya, maka data akan ditambahkan.',mtCustom,
                              //[mbYes,mbCancel], 0)=mryes)then
//          if(add_double_data)then
            istableexist:=false
//          else
//          istableexist:=true;
        end;
      end;

      if(not istableexist)then
      begin
        //INSERT tanggal
        tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
        with ZQuery1 do  //masukkin nih gan
          begin
            Active:=False;
            sql.Clear;
            sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
            ExecSQL;
          end;
        ZTable1.Active := False;
        ZTable1.Filtered := False;
        ZTable1.TableName := 'tanggal';
        ZTable1.SortedFields:='ID_TANGGAL';
        ZTable1.SortType := stDescending;
        ZTable1.Active := True;
        ZTable1.First;
        idtgl:=ZTable1.Fields[1].AsString;
        //ShowMessage('tgl='+idtgl);
        ZTable1.SortedFields:='';
        //CREATE TABLE CUY
        with ZQuery1 do  //masukkin nih gan
          begin
            //pemilih
            Active:=False;
            sql.Clear;
            sql.Text := 'CREATE TABLE IF NOT EXISTS dpt_'+ikdesa+'('+
                          'ID_USER int(11) NOT NULL AUTO_INCREMENT,'+
                          'NIK char(36) NOT NULL,'+
                          'NKK char(36) NOT NULL,'+
                          'NAMA char(50) NOT NULL,'+
                          'TEMPAT char(36) NOT NULL,'+
                          'TANGGAL char(10) NOT NULL,'+
                          'IBU char(36) NOT NULL,'+
                          'AGAMA char(36) NOT NULL,'+
                          'KAWIN char(1) NOT NULL,'+
                          'KELAMIN char(1) NOT NULL,'+
                          'KELAINAN char(36) NOT NULL,'+
                          'ALAMAT char(150) NOT NULL,'+
                          'RW char(3) NOT NULL,'+
                          'RT char(3) NOT NULL,'+
                          'DUSUN char(30) NOT NULL,'+
                          'KET char(150) NOT NULL,'+
                          'ID_TANGGAL int(11) NOT NULL,'+
                          'TPS int(11) NOT NULL,'+
                          'STATUS1 char(1) NOT NULL,'+
                          'STATUS2 char(1) NOT NULL,'+
                          'GANDA char(1) NOT NULL,'+
                          'PRIMARY KEY (ID_USER))';
            ExecSQL;
            //penyimpan
            Active:=False;
            sql.Clear;
            //ubah
            sql.Text := 'INSERT IGNORE INTO penyimpan_dpt (NAMA,UPLOAD) VALUES ("dpt_'+ikdesa+'",'+QuotedStr(StripCharsInSet(namafile,[#10..#32,#44..#57,#65..#93,#95..#122]))+')';
            ExecSQL;
          end;
        //Indexing
        with ZQuery1 do
        begin
          Active:=False;
          SQL.Clear;
          SQL.Text:='SELECT ID FROM penyimpan_dpt where NAMA = "dpt_'+ikdesa+'"';
          Active:=True;
          First;
        end;
        idsimpan:=ZQuery1.Fields[0].AsString;
      end;
    //PATH_end
    if(not istableexist)then
    begin
      if ADOConnection1.Connected then
      begin
        ADOConnection1.Close;
      end;
      // Build connection string using the Microsoft Access Database Engine 2010
      sConnectionString :=
        'Provider=Microsoft.ACE.OLEDB.12.0;' +
        'Data Source="' + namafile + '";' +
        'Extended Properties="Excel 8.0;HDR=NO;"';
      // Assign the connection string to the ADO component
      ADOConnection1.ConnectionString := sConnectionString;
      ADOConnection1.Connected := True;
      // Connect to the provider
      slTables := TStringList.Create;
      try
        // Get list of sheets (as tables)
        ADOConnection1.GetTableNames(slTables);
        // Go through each sheet(table) and query it
        ADOQuery1.Connection := ADOConnection1;
        //for i := 0 to slTables.Count - 1 do
       // begin
          // Query the sheet(table)
          ADOQuery1.Active := False;
          ADOQuery1.SQL.Text := 'SELECT * FROM `' + slTables.Strings[0] + '`';
          ADOQuery1.Active := True;
          if not ADOQuery1.IsEmpty then
          begin
            // There were rows
            tulis := 0;
            try
              // Loop through the rows and output to CSV
              ADOQuery1.First;
              rcount:=0;
              //tampilan proses
              sTemp:='INSERT INTO dpt_'+ikdesa+' (';
              for k := 1 to 15 do begin
                if ((k<>10) and (kotak[k]<>-1)) then begin
                  case k of
                    1 : begin
                      sTemp:=sTemp+'NIK,';
                    end;
                    2 : begin
                      sTemp:=sTemp+'NKK,';
                    end;
                    3 : begin
                      sTemp:=sTemp+'NAMA,';
                    end;
                    4 : begin
                      sTemp:=sTemp+'TEMPAT,';
                    end;
                    5 : begin
                      sTemp:=sTemp+'TANGGAL,';
                    end;
                    6 : begin
                      sTemp:=sTemp+'IBU,';
                    end;
                    7 : begin
                      sTemp:=sTemp+'AGAMA,';
                    end;
                    8 : begin
                      sTemp:=sTemp+'KAWIN,';
                    end;
                    9 : begin
                      sTemp:=sTemp+'KELAMIN,';
                    end;
                    11 : begin
                      sTemp:=sTemp+'KELAINAN,';
                    end;
                    12 : begin
                      sTemp:=sTemp+'ALAMAT,';
                    end;
                    13 : begin
                      sTemp:=sTemp+'RW,';
                    end;
                    14 : begin
                      sTemp:=sTemp+'RT,';
                    end;
                    15 : begin
                      sTemp:=sTemp+'DUSUN,';
                    end;
                    16 : begin
                      sTemp:=sTemp+'KET,';
                    end;
                  end; //case
                end;  //if
              end;  //for
              sTemp:=sTemp+'ID_TANGGAL,TPS,STATUS1,STATUS2,GANDA) VALUES ';
              sLine := '';
              counterbaris:=0;
              //insert into
              while not ADOQuery1.Eof do
              begin
                ins:=false;
                // Loop through the columns

                // kasih tau ke user lagi ngapain
                JvThreadSimpleDialog1.DialogOptions.InfoText:=namafile+sLineBreak+'jumlah baris : '+inttostr(counterbaris);
                counterbaris:=counterbaris+1; // jumlah baris yang dibaca

                for j := 0 to ADOQuery1.FieldCount-1 do
                begin
                  fField := ADOQuery1.Fields[j];
                  sel:=fField.AsString;
                  //IDE PROGRAM
                  if ((j=kotak[1]) and (sel='NIK')) then  //cari header PENTING
                    begin
                      tulis:=1;
                      break;
                    end;
                  if (tulis=1) then   //sudah ketemu header
                    begin
                      if ((Length(sel)>0) and (j=kotak[1])) then
                        begin
                          tulis:=2;
                          if (Length(sel)=1) then
                            break;
                        end;
                    end;
                  if tulis=2 then //masuk isi
                    begin
                      ins:=true;
                      if ((kotak[1]>-1) and (j=kotak[1])) then
                          isi[1]:=sel;
                      if ((kotak[2]>-1) and (j=kotak[2])) then
                          isi[2]:=sel;
                      if ((kotak[3]>-1) and (j=kotak[3])) then
                          isi[3]:=sel;
                      if ((kotak[4]>-1) and (kotak[5]>-1) and (cbTempat.ItemIndex=cbTgl.ItemIndex)) then
                        begin
                          if (j=kotak[4]) then
                            begin
                              ExtractStrings([','], [], PChar(sel), stTemp);
                              isi[4] := stTemp[0];
                              isi[5] := stTemp[1];
                            end;
                        end
                      else
                        begin
                          if ((kotak[4]>-1) and (j=kotak[4])) then
                              isi[4]:=sel;
                          if ((kotak[5]>-1) and (j=kotak[5])) then
                              isi[5]:=sel;
                        end;
                      if ((kotak[6]>-1) and (j=kotak[6])) then
                          isi[6]:=sel;
                      if ((kotak[7]>-1) and (j=kotak[7])) then
                          isi[7]:=sel;
                      if ((kotak[8]>-1) and (j=kotak[8])) then
                          isi[8]:=sel;
                      if ((kotak[9]>-1) and (kotak[10]>-1)) then
                        begin
                          if ((j=kotak[9]) and (sel<>'')) then
                            isi[9]:='L'
                          else
                            isi[9]:='P';
                        end
                        else
                        begin
                          if ((kotak[9]>-1) and (kotak[10]<0) and (j=kotak[9])) then
                            isi[9]:=sel;
                        end;
                      if ((kotak[11]>-1) and (j=kotak[11])) then
                          isi[10]:=sel;
                      if ((kotak[12]>-1) and (j=kotak[12])) then
                          isi[11]:=sel;
                      if ((kotak[13]>-1) and (j=kotak[13])) then
                          isi[12]:=sel;
                      if ((kotak[14]>-1) and (j=kotak[14])) then
                          isi[13]:=sel;
                      if ((kotak[15]>-1) and (j=kotak[15])) then
                          isi[14]:=sel;
                      if ((kotak[16]>-1) and (j=kotak[16])) then
                          isi[15]:=sel;
                      //kalau ada isian kosong gimana?
                      if ((Length(sel)<1) and (j=kotak[1])) then
                        begin
                          tulis:=0;
                          ins:=false;
                          break;
                        end;
                    end;
                end;
                if (ins=True) then begin
                  Inc(iduser);
                  sLine:=sLine+'(';
                  for k := 1 to 15 do begin
                    if ((k<>10) and (kotak[k]<>-1)) then begin
                      case k of
                        1 : begin
                          sLine:=sLine+QuotedStr(Trim(isi[1]))+',';
                        end;
                        2 : begin
                          sLine:=sLine+QuotedStr(Trim(isi[2]))+',';
                        end;
                        3 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[3],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        4 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[4],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        5 : begin
                          sLine:=sLine+QuotedStr(Trim(isi[5]))+',';
                        end;
                        6 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[6],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        7 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[7],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        8 : begin
                          sLine:=sLine+QuotedStr(Trim(isi[8]))+',';
                        end;
                        9 : begin
                          sLine:=sLine+QuotedStr(Trim(isi[9]))+',';
                        end;
                        11 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[10],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        12 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[11],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        13 : begin
                          rwt:=Trim(isi[12]);
                          if (rwt='') or (rwt='-') or (rwt='0') then
                            rwt:='000';
                          rwt:=cekRTRW(rwt);
                          sLine:=sLine+QuotedStr(rwt)+',';
                        end;
                        14 : begin
                          rwt:=Trim(isi[13]);
                          if (rwt='') or (rwt='-') or (rwt='0') then
                            rwt:='000';
                          rwt:=cekRTRW(rwt);
                          sLine:=sLine+QuotedStr(rwt)+',';
                        end;
                        15 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[14],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                        16 : begin
                          sLine:=sLine+QuotedStr(StripCharsInSet(isi[15],[#10..#32,#44..#46,#48..#57,#65..#90,#97..#122]))+',';
                        end;
                      end; //case
                    end;  //if kotak
                  end;  //for
                  sLine:=sLine+idtgl+',0'+','+QuotedStr('0')+','+QuotedStr('0')+','+QuotedStr('0')+'),';
                  Inc(rcount);
                end;  //if ins
                ADOQuery1.Next;
                if (rcount>999) then begin
                  //masukkin query
                  sLine := Copy(sLine,0,(Length(sLine)-1));
                  sLine := sTemp+sLine+';';
                  with ZQuery1 do  //masukkin nih gan
                    begin
                      Active:=False;
                      sql.Clear;
                      sql.Text := sLine;
                      ExecSQL;
                    end;
                  //reset
                  sLine:='';
                  rcount:=0;
                end;
              end;
              finally
                //free csv
                tulis := 0;
              end;
          end;
        //end;
              ADOQuery1.Active := False;
      finally
      end;
      if (sLine<>'') then begin
        //masukkin query
        sLine := Copy(sLine,0,(Length(sLine)-1));
        sLine := sTemp+sLine+';';
        with ZQuery1 do  //masukkin nih gan
          begin
            Active:=False;
            sql.Clear;
            sql.Text := sLine;
            ExecSQL;
          end;
        //reset
        sLine:='';
        rcount:=0;
      end;
      // FIX ME ?
      ADOConnection1.Close;
      namatabel:='dpt_'+ikdesa;
      //INDEXING
      if(debug) then ShowMessage('START INDXING');
      with ZTable1 do
      begin
        Active:=False;
        TableName:=namatabel;
        Active:=True;
        ext:= ExternalFile.Create;
//        ext.ztableIndexCreator(ZTable1,StrToInt(idsimpan));
      end;
      //---------
    end;
end;

procedure Tdpt_import_form.JvThread1Execute(Sender: TObject; Params: Pointer);
var
  i, j, k: Integer;

begin
  //Do the job here
  k := 0;
//  for i := 0 to 100 do
//    for j := 0 to 10 do
//    begin
//      Inc(k,5); //This is the only difference with the other thread
//      //sleep(13);
//      //JvThread1.Synchronize();
//      end;
      //begin
      //To use global variable/objects, you have to synchronize (to avoid conflicts)
      //Tdpt_import_form(params).importfileexcel();
            //
             CoInitialize(nil); // <-- manually call CoInitialize()



      for I := 0 to listexceldata.Count - 1 do
        begin
          JvThreadSimpleDialog1.DialogOptions.InfoText:=listexceldata[i];
          //ext.importfileexcel(listexceldata[i],listexceldesa[i], ztable1,zquery1,adoconnection1,adoquery1, cbTempat.ItemIndex,cbTgl.ItemIndex);
          Tdpt_import_form(params).importfileexcel(listexceldata[i],listexceldesa[i]);
        end;
      //lblProses.Caption:='Selesai';
      //ShowMessage('Excel '+namatabel+' selesai diproses');
//    end;
    Tdpt_import_form(params).refreshdesa();

      //JvThread2.Synchronize(TForm1(params).Stats2);

  CoUnInitialize; //

      if JvThread1.Terminated then
      begin

        Exit;
      end;
    //end;

end;

procedure Tdpt_import_form.JvThread2Execute(Sender: TObject; Params: Pointer);
const
  xlCellTypeLastCell = $0000000B;
var  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
 CoInitialize(nil); // <-- manually call CoInitialize()

    XLApp := CreateOleObject('Excel.Application');
    try
      XLApp.Visible := False;
      XLApp.Workbooks.Open(sFile);
      Sheet := XLApp.Workbooks[ExtractFileName(sFile)].WorkSheets[1];
      Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
      x := XLApp.ActiveCell.Row;
      y := XLApp.ActiveCell.Column;

      RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;

      preview.ColCount:=y;
      preview.RowCount:=50;
      for r := 0 to y do
        begin
        preview.Cells[r,0]:=Chr(65+r);


        end;
      k := 1;
      repeat
        for r := 1 to y do
        begin
          //this section to operate data, change as your need
          preview.Cells[r-1,k]:=RangeMatrix[K, R];
        end;
        Inc(k, 1);
      until ((k > x) or (k>50));

      RangeMatrix := Unassigned;

    finally
      if not VarIsEmpty(XLApp) then
      begin
        XLApp.Quit;
        XLAPP := Unassigned;
        Sheet := Unassigned;
      end;
    end;
    for i := 0 to preview.ColCount - 1 do
      AutoSizeCol(preview, i);

    CoUnInitialize; //


    if JvThread2.Terminated then
      Exit;
end;

Function Tdpt_import_form.cekRTRW(kata:String) : String;
var
  stemp:String;
begin
  sTemp:='';
  if (Length(kata)=1) then
    sTemp:='00'+kata
  else if (Length(kata)=2) then
    sTemp:='0'+kata
  else if (Length(kata)>3) then
    sTemp:=Copy(kata, Length(kata)-3, 3)
  else
    sTemp:=kata;
  Result := sTemp;
end;

procedure Tdpt_import_form.cbAgamaChange(Sender: TObject);
begin
  kotak[7]:=cbAgama.ItemIndex-1;
end;

procedure Tdpt_import_form.cbAlamatChange(Sender: TObject);
begin
  kotak[12]:=cbAlamat.ItemIndex-1;
end;

procedure Tdpt_import_form.cbDusunChange(Sender: TObject);
begin
  kotak[15]:=cbdusun.ItemIndex-1;
end;

procedure Tdpt_import_form.cbIbuChange(Sender: TObject);
begin
  kotak[6]:=cbIbu.ItemIndex-1;
end;

procedure Tdpt_import_form.cbKawinChange(Sender: TObject);
begin
  kotak[8]:=cbKawin.ItemIndex-1;
end;

procedure Tdpt_import_form.cbKelainanChange(Sender: TObject);
begin
  kotak[11]:=cbKelainan.ItemIndex-1;
end;

procedure Tdpt_import_form.cbKelamin2Change(Sender: TObject);
begin
  kotak[10]:=cbKelamin2.ItemIndex-1;
end;

procedure Tdpt_import_form.cbKelaminChange(Sender: TObject);
begin
  kotak[9]:=cbKelamin.ItemIndex-1;
end;

procedure Tdpt_import_form.cbKetChange(Sender: TObject);
begin
  kotak[16]:=cbKet.ItemIndex-1;
end;

procedure Tdpt_import_form.cbNamaChange(Sender: TObject);
begin
  kotak[3]:=cbNama.ItemIndex-1;
end;

procedure Tdpt_import_form.cbNIKChange(Sender: TObject);
begin
  kotak[1]:=cbNIK.ItemIndex-1;
end;

procedure Tdpt_import_form.cbNKKChange(Sender: TObject);
begin
  kotak[2]:=cbNKK.ItemIndex-1;
end;

procedure Tdpt_import_form.cbRtChange(Sender: TObject);
begin
  kotak[14]:=cbRt.ItemIndex-1;
end;

procedure Tdpt_import_form.cbRwChange(Sender: TObject);
begin
  kotak[13]:=cbRw.ItemIndex-1;
end;

procedure Tdpt_import_form.cbTempatChange(Sender: TObject);
begin
  kotak[4]:=cbTempat.ItemIndex-1;
end;

procedure Tdpt_import_form.cbTglChange(Sender: TObject);
begin
  kotak[5]:=cbTgl.ItemIndex-1;
end;

procedure Tdpt_import_form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;
procedure Tdpt_import_form.refreshDesa();
begin
  with Ztable1 do
  begin
    active := false;
    tablename:='penyimpan';
    active:=true;
    desa_terimport.Caption := inttostr(RecordCount)+' desa telah diimport';
    first;
  end;
end;


procedure Tdpt_import_form.FormCreate(Sender: TObject);
var
  ch:Char;
  setting:TSetting_Form;
begin
//  WindowState:=wsMaximized;
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;

  listexceldata:=tstringlist.Create;
  listexceldesa:=tstringlist.Create;
  listnamakota:=tstringlist.Create;
  listnamakec:=tstringlist.Create;
  listnamadesa:=tstringlist.Create;
  refreshGrid;

  namakota:='';
  namakec:='';
  namadesa:='';
  MessageDlg('Fasilitas import data tidak disarankan untuk melakukan update, jika ada update silakan dilakukan di fasilitas EDIT DATA PEMILIH di form VIEW DATA PEMILIH',mtCustom,
                            [mbYes], 0);
  refreshDesa();
  with Ztable1 do
  begin
    active := false;
    tablename:='penyimpan';
    active:=true;
    desa_terimport.Caption := inttostr(RecordCount)+' desa telah diimport';
  end;
  //generate dropdown kabkot
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    if(kode_dropdown<>'00')then // kasus khusus bila user petugas kabkot
    begin
      filter:='KODE='+QuotedStr(kode_dropdown);
      filtered:=true;
    end;
    Active := True;
    First;
  end;
  cbKabkot.Clear;
  cbKec.Clear;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbKabkot.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;

  if(kode_dropdown<>'00')then // bila user adalah petugas kabkot maka tidak bisa mengganti kota
  begin
    cbKabkot.ItemIndex:=0; // default kabupaten
    cbKabkot.Enabled:=false;
    ikabkot:=kode_dropdown;
    with ZTable1 do
    begin
      first;
      namakota:=FieldByName('TAMPIL').AsString; // isi nama kota dulu

      Active := False;
      TableName := 'kecamatan';
      filter:='KODE_KABKOT='+QuotedStr(kode_dropdown);

      filtered:=true;
      Active := True;
      First;
      while (not eof) do
      begin
        cbKec.Items.Add(FieldByName('TAMPIL').AsString);
        next;
      end;
    end;
  end;
  cbKelamin2.Enabled:=True;
  ch:=' ';
    cbNIK.Items.Add(ch);
    cbNKK.Items.Add(ch);
    cbNama.Items.Add(ch);
    cbTempat.Items.Add(ch);
    cbTgl.Items.Add(ch);
    cbIbu.Items.Add(ch);
    cbAgama.Items.Add(ch);
    cbKawin.Items.Add(ch);
    cbKelamin.Items.Add(ch);
    cbKelamin2.Items.Add(ch);
    cbKelainan.Items.Add(ch);
    cbAlamat.Items.Add(ch);
    cbRw.Items.Add(ch);
    cbRt.Items.Add(ch);
    cbDusun.Items.Add(ch);
    cbKet.Items.Add(ch);
  ch:=#65;
  while (ch<#81) do
    begin
      cbNIK.Items.Add(ch);
      cbNKK.Items.Add(ch);
      cbNama.Items.Add(ch);
      cbTempat.Items.Add(ch);
      cbTgl.Items.Add(ch);
      cbIbu.Items.Add(ch);
      cbAgama.Items.Add(ch);
      cbKawin.Items.Add(ch);
      cbKelamin.Items.Add(ch);
      cbKelamin2.Items.Add(ch);
      cbKelainan.Items.Add(ch);
      cbAlamat.Items.Add(ch);
      cbRw.Items.Add(ch);
      cbRt.Items.Add(ch);
      cbDusun.Items.Add(ch);
      cbKet.Items.Add(ch);
      Inc(ch);
    end;
    cbNIK.Text:='';
    cbNKK.Text:='';
    cbNama.Text:='';
    cbTempat.Text:='';
    cbTgl.Text:='';
    cbIbu.Text:='';
    cbAgama.Text:='';
    cbKawin.Text:='';
    cbKelamin.Text:='';
    cbKelamin2.Text:='';
    cbKelainan.Text:='';
    cbAlamat.Text:='';
    cbRw.Text:='';
    cbRt.Text:='';
    cbDusun.Text:='';
    cbKet.Text:='';
    SetLength(kotak,17);
    for i := 0 to Length(kotak) - 1 do
      kotak[i]:=-1;
    SetLength(isi,16);
    for i := 0 to Length(isi) - 1 do
      isi[i]:='';
    lblProses.Caption:='';
end;

procedure Tdpt_import_form.FormDestroy(Sender: TObject);
begin
  if not JvThread1.Terminated then
    JvThread1.Terminate;
  while not (JvThread1.Terminated) do
  begin
    Sleep(100);
    Application.ProcessMessages;
  end;

end;

procedure Tdpt_import_form.HapusClick(Sender: TObject);
begin
  if (sfile<>'') and (sdesa<>'') then begin
    listexceldata.Delete(listexcel.Row-1);
    listexceldesa.Delete(listexcel.Row-1);
    refreshGrid;
    sfile:='';
    sdesa:='';
  end;
end;

procedure Tdpt_import_form.listexcelClick(Sender: TObject);
begin
  if(listexcel.Row<>0)then
  begin
    sfile:=listexceldata[listexcel.Row-1];
    sdesa:=listexceldesa[listexcel.Row-1];
  end
    else
  begin
    sfile:='';
    sdesa:='';
  end
end;

end.
